package com.game77.guessTheWords2.scene;

import android.view.KeyEvent;

import com.game77.guessTheWords2.core.StageManager;
import com.game77.guessTheWords2.layer.GameLayer;
import com.game77.guessTheWords2.util.ADHelper;
import com.game77.guessTheWords2.util.GameUtil;
import com.wiyun.engine.nodes.Director;
import com.wiyun.engine.nodes.Scene;

/**
 * 游戏场景
 * 
 * @author davelin
 * 
 */
public class GameScene extends Scene {
	private static GameScene instance = null;
	private static Object sync_obj = new Object();

	GameLayer gameLayer;

	protected GameScene() {
		gameLayer = new GameLayer();
		gameLayer.setTouchEnabled(true);
		addChild(gameLayer);
	}

	public static GameScene make() {
		synchronized (sync_obj) {
			if (instance == null) {
				instance = new GameScene();
			}
			return instance;
		}
	}

	public void returnToStageScene() {
		GameUtil.switchSceneFlipX(StageScene.make());
		StageScene.make().refreshStages();
		gameLayer.endGame();
		ADHelper.hideAd();
		saveStageDataAndSubmitScore();
	}

	public void goToToolShopScene() {
		GameUtil.switchSceneFlipX(ToolShopScene.make());
		gameLayer.endGame();
		ADHelper.hideAd();
		saveStageDataAndSubmitScore();
	}

	private void saveStageDataAndSubmitScore() {
		Director.getInstance().runThread(new Runnable() {
			@Override
			public void run() {
				// 保存关卡数据
				StageManager.instance
						.saveCurrentStageData(StageManager.instance
								.getLastClass()
								+ "_"
								+ StageManager.instance.getLastStage());
			}
		});
	}

	@Override
	public boolean wyKeyDown(KeyEvent event) {

		if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
			returnToStageScene();
			return true;
		}
		return super.wyKeyDown(event);
	}
}
